package cn.itcast.bos.mq;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;

import org.springframework.stereotype.Service;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;

import cn.itcast.bos.utils.AliSmsUtils;
import cn.itcast.bos.utils.SmsUtils;

@Service("smsConsumer")
public class SmsConsumer implements MessageListener {

	@Override
	public void onMessage(Message message) {
		MapMessage mapMessage = (MapMessage) message;
		
		// 调用SMS服务发送短信      15072868610
//		try {
//			System.out.println("消费者："+mapMessage.getString("telephone2")+" "+mapMessage.getString("msg2"));
//			 String result =  SmsUtils.sendSmsByHTTP(mapMessage.getString("telephone2"),mapMessage.getString("msg2"));
//			 System.out.println("SmsConsumer结果="+result);
////			 SmsConsumer结果=000/Send:1/Consumption:.1/Tmoney:1.4/sid:0505184305427107
////			  发送短信成功,手机号：15868447548，短信内容：尊敬的用户您好，本次获取的验证码为：3047,服务电话：4006184000
//			if (result.startsWith("000")) {
//				// 发送成功
//				System.out.println(
//						"发送短信成功,手机号：" + mapMessage.getString("telephone") + 
//						"，短信内容：" + mapMessage.getString("msg"));
//			} else {
//				// 发送失败
//				throw new RuntimeException("短信发送失败, 短信内容：" + result);
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		
		SendSmsResponse response = null;
		try {
			System.out.println("消费者：" + mapMessage.getString("telephone2")
					+ " " + mapMessage.getString("username2") + " "
					+ mapMessage.getString("randomCode2"));
			response = AliSmsUtils.sendSms(
					mapMessage.getString("telephone2"),
					mapMessage.getString("username2"),
					mapMessage.getString("randomCode2"));
			if (response.getCode() != null && response.getCode().equals("OK")) {
				System.out.println("发送短信成功!");
			}
			
		} catch (Exception e) {
			// 发送失败
			throw new RuntimeException("短信发送失败="+response);
		}

	}
}
